package com.app.Interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.app.entity.BackendUser;

public class SecurityInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		
		//获取session
		HttpSession session =request.getSession();
		BackendUser user =(BackendUser)session.getAttribute("user");
		if(user==null){
			//自动跳转到登录
			String requestPath =request.getServletPath();
			System.out.println(requestPath);
			//获取地址栏查询字符串?后面的部分
			String query =request.getQueryString();//拿到问号后面所有的对象
			System.out.println(query);
			String url =request.getContextPath()+"/login?returnUrl="+requestPath;
			if (query !=null) {
				url+="?"+query;
			}
			response.sendRedirect(url);
			return false;
		}
				return true;
	

	}
}
